package com.scwang.smart.drawer.exercise.adapter

import android.text.Html
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.TextView
import com.scwang.smart.drawer.SmartDrawerLayout
import com.scwang.smart.drawer.app.R
import com.scwang.smart.drawer.constant.DrawerDragStyle
import com.scwang.smart.drawer.exercise.model.Example
import com.scwang.smart.drawer.exercise.model.Subject

class ExercisePagerGroup(parent: ViewGroup, private val mExample: Example) : ExercisePagerChoice(
    LayoutInflater.from(parent.context)
        .inflate(R.layout.holder_exercise_pager_group, parent, false), mExample
) {
    private var mWebViewAbstract: WebView = itemView.findViewById(R.id.pager_group_abstract)
    private var mWebViewAbstractText: TextView = itemView.findViewById(R.id.pager_group_abstract_text)
    private var mDrawerLayout: SmartDrawerLayout = itemView.findViewById(R.id.pager_group_drawer)

    init {
        val settings = mWebViewAbstract.settings
        settings.javaScriptEnabled = false
        settings.setAppCacheEnabled(false)
        settings.databaseEnabled = false
        settings.domStorageEnabled = false
        settings.setGeolocationEnabled(false)
        settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        mWebViewAbstractText.movementMethod = ScrollingMovementMethod.getInstance()

        mDrawerLayout.setPanelStyle(DrawerDragStyle.Stretch)
    }

    override fun onBind(subject: Subject, position: Int) {
        super.onBind(subject, position)
        val html: String = subject.GroupContent
        val matcher = matcherHtmlText(mPatternSubject, html)
        if (matcher.find()) {
            mWebViewAbstract.visibility = View.GONE
            mWebViewAbstractText.visibility = View.VISIBLE
            mWebViewAbstractText.text = Html.fromHtml(matcher.group(1))
            //$(R.id.pager_group_abstract_text).marginBottom(height)
        } else {
            mWebViewAbstract.visibility = View.VISIBLE
            mWebViewAbstractText.visibility = View.GONE
            mWebViewAbstract.loadData(html, "text/html;charset=UTF-8", null)
            mWebViewAbstract.setBackgroundColor(0)
            //$(mWebViewAbstract).marginBottom(height)
        }
    }
}